<template>
  <Dialog
    v-model="show"
    @confirm="handleSubmit"
    @close="close"
    :title="t('service.审核退款申请')"
    width="600px"
    :confirmLoading="confirmLoading"
  >
    <div class="h-500 overflow-y-auto">
      <el-form ref="formRef" :model="form" label-width="120px" :rules="rules">
        <el-form-item :label="t('service.是否同意申请')">
          <el-radio-group v-model="form.refundStatus">
            <el-radio :label="-3">{{ t("service.同意") }}</el-radio>
            <el-radio :label="-4">{{ t("service.驳回") }}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item
          :label="t('service.驳回原因')"
          v-if="form.refundStatus === -4"
          prop="refundCheckRejectReason"
        >
          <el-input
            v-model="form.refundCheckRejectReason"
            type="textarea"
            :rows="3"
            :placeholder="t('service.请输入')"
          />
        </el-form-item>
        <el-form-item
          :label="t('service.退款金额')"
          v-if="form.refundStatus === -3"
        >
          <div>
            <el-input-number
              v-model="form.refundCheckPayTotal"
              :min="0"
              :max="refundRealTotal"
              style="width: 100%"
            />
            <div class="fs-12 color-666666">
              {{ t("service.请输入退款金额，最多") }}{{ props.detail?.refundAskPayTotal }}{{ t("currency.元") }}。
              <!-- {{
                t("service.商品实付金额")
              }}{{ refundRealSku }}{{ t("currency.元") }}；{{
                t("service.商品打包费")
              }}{{ refundRealPostage }}{{ t("currency.元") }}；{{ t("service.总实付金额")
              }}{{ refundRealTotal }}{{ t("currency.元") }} -->
            </div>
          </div>
        </el-form-item>
      </el-form>
    </div>
  </Dialog>
</template>
<script setup>
import { ref, watch, watchEffect } from "vue";
import Dialog from "@/components/dialog.vue";
import Api from "@/config/api/api";
import { useI18n } from "vue-i18n";
import { ElMessage } from "element-plus";

const { t } = useI18n();
const show = defineModel();
const props = defineProps({
  detail: {
    type: Object,
    default: () => ({}),
  },
});
const emit = defineEmits(["rest"]);
const refundRealTotal = ref(0);
const refundRealPostage = ref(0);
const refundRealSku = ref(0);
const confirmLoading = ref(false);

watchEffect(() => {
  const { saleSkus } = props.detail || [];
  const payRealSku = saleSkus?.reduce((acc, curr) => {
    return acc + curr.payRealSku;
  }, 0);
  const skuPackingFee = saleSkus?.reduce((acc, curr) => {
    return acc + curr.skuPackingFee * curr.skuPackingFee;
  }, 0);
  refundRealTotal.value = (payRealSku || 0) + (skuPackingFee || 0);
  refundRealSku.value = payRealSku;
  refundRealPostage.value = skuPackingFee;
  // refundAskPayTotal.value = props.detail?.refundAskPayTotal;
});
const form = ref({
  refundStatus: -3,
  refundCheckRejectReason: "",
  refundCheckAddressId: "",
  refundCheckPayTotal: props.detail?.refundAskPayTotal, //  这里先取用户申请的退款金额
});
const rules = {
  refundCheckRejectReason: [
    { required: true, message: t("service.请输入驳回原因"), trigger: "blur" },
  ],
};
const formRef = ref();
watch(show, (val) => {
  if (val) {
  }
});
const handleSubmit = () => {
  formRef.value.validate((valid, fields) => {
    if (valid) {
      confirmLoading.value = true;

      const callback = (res) => {
        if (res.status === 200) {
          ElMessage.success(t("service.操作成功"));
          show.value = false;
          emit("rest");
        }
      };

      // 仅退款申请审核
      Api.refundCheck({
        id: props.detail.id,
        refundStatus: form.value.refundStatus,
        refundCheckRejectReason: form.value.refundCheckRejectReason,
        refundCheckPayTotal: form.value.refundCheckPayTotal,
      })
        .then((res) => {
          callback(res);
        })
        .finally(() => {
          confirmLoading.value = false;
        });
    }
  });
};
const close = () => {
  form.value = {
    refundStatus: -3,
    refundCheckRejectReason: "",
    refundCheckAddressId: "",
    refundCheckPayTotal: props.detail?.refundAskPayTotal,
  };
  show.value = false;
};
</script>
<style lang="scss" scoped>
.h-500 {
  height: 500px;
}
.lht-38 {
  line-height: 38px;
}
.w-120 {
  width: 120px;
}
.address {
  .address-item {
    padding: 10px;
    border: 1px solid #ccc;
    border-radius: 5px;
    margin-bottom: 10px;
    .address-item-address {
      font-size: 12px;
      color: #999;
    }

    &:hover {
      border-color: #1890ff;
    }
    &.active {
      border-color: #1890ff;
    }
  }
}
</style>
